﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLogic
{
   public class UserCommentLogic
    {
       Model1Container db = new Model1Container();


       public int DelInfo(List<string> list)
       {
           var arry = list.ToArray();
           int result = 0;
           try
           {
               using (Model1Container db = new Model1Container())
               {
                   var listm = db.UserComment.Where(t => arry.Contains(t.ID));
                   foreach (var item in listm)
                   {
                       item.IsDelete = 1;
                   }
                   db.SaveChanges();
                   result = 1;
               }
           }
           catch (Exception ex)
           {
               return 0;
           }
           return result;
       }
       public V_Comment GetFormInfo(string ID)
       {
           V_Comment v_model = new V_Comment();
           try
           {
               Model1Container db = new Model1Container();
               SqlParameter[] para = new SqlParameter[] { new SqlParameter("@CommentID", SqlDbType.NVarChar, 50) { Value = ID }};
               var result = db.Database.SqlQuery<PostClass>(@"  select aa.Content,aa.CommentDate,aa.Star,bb.Tel as UserTel from UserComment aa inner join UserInfo bb on aa.UserID=bb.ID where aa.ID=@CommentID", para).ToList().First();

               v_model.CommentDate = result.CommentDate;
               v_model.Content = result.Content;
               v_model.UserTel = result.UserTel;
               v_model.Star = result.Star;
           }
           catch (Exception ex)
           {
               return null;
           }

           return v_model;
       }

       public int Agree(List<string> list)
       {
           var arry = list.ToArray();
           int result = 0;
           try
           {
               using (Model1Container db = new Model1Container())
               {
                 var listm = db.UserComment.Where(t => arry.Contains(t.ID));
                  foreach (var item in listm)
                  {
                      item.CheckStatus = 1;
                  }
                  db.SaveChanges();
                  result = 1;
               }
           }
           catch (Exception ex)
           {
               return 0;
           }

           return result;
       }
       public int Refuse(List<string> list)
       {
           var arry = list.ToArray();
           int result = 0;
           try
           {
               using (Model1Container db = new Model1Container())
               {
                   var listm = db.UserComment.Where(t => arry.Contains(t.ID));
                   foreach (var item in listm)
                   {
                       item.CheckStatus = 2;
                       item.CheckDate = DateTime.Now;
                   }
                   db.SaveChanges();
                   result = 1;
               }
           }
           catch (Exception ex)
           {
               return 0;
           }
           return result;
       }
    }
}
public class PostClass
{
    public DateTime CommentDate { get; set; }
    public string Content { get; set; }
    public string UserTel { get; set; }
    public byte? Star { get; set; }

}
